package com.qidi.mapper.impl;

import com.qidi.mapper.BookDao;
import com.qidi.pojo.Book;

import java.util.List;

/**
 * @author zhouXinBin
 * @create 2020-09-02-16:34
 */
public class BookDaoimpl extends BaseDao implements BookDao {
    @Override
    public Book queryBookById(Integer id) {
        String sql="select id,name,price,author,sales,stock,img_path imgPath from t_book where id=?";
        return queryForOne(Book.class,sql,id);
    }

    @Override
    public List<Book> queryAllBook() {
        String sql="select id,name,price,author,sales,stock,img_path imgPath from t_book";
        return queryForList(Book.class,sql);
    }

    @Override
    public void addBook(Book book) {
        String sql="insert into t_book(name,price,author,sales,stock,img_path) values(?,?,?,?,?,?)";
        update(sql,book.getName(),book.getPrice(),book.getAuthor(),book.getSales(),book.getStock(),book.getImgPath());
    }

    @Override
    public void deleteBookById(Integer id) {
        String sql="delete from t_book where id=?";
        update(sql,id);
    }

    @Override
    public void updateBook(Book book) {
        String sql="update t_book set name=?,price=?,author=?,sales=?,stock=?,img_path=? where id=?";
        update(sql,book.getName(),book.getPrice(),book.getAuthor(),book.getSales(),book.getStock(),book.getImgPath(),book.getId());
    }

    @Override
    public List<Book> queryPageBook(int begin, int pageSize) {
        String sql="select id,name,price,author,sales,stock,img_path imgPath from t_book limit ?,?";
        return queryForList(Book.class,sql,begin,pageSize);
    }

    @Override
    public Object TotalBooknum() {
        String sql="select count(*) from t_book";
        Number o = (Number) queryForSingleValue(sql);
        return o.intValue();
    }

    @Override
    public Object BooknumByPrice(int min, int max) {
        String sql="select count(*) from t_book where price between ? and ?";
        Number o = (Number) queryForSingleValue(sql,min,max);
        return o.intValue();
    }

    @Override
    public List<Book> queryPageBookByPrice(int begin, Integer pageSize, int min, int max) {
        String sql="select id,name,price,author,sales,stock,img_path imgPath from t_book where price between ? and ? limit ?,?";
        return queryForList(Book.class,sql,min,max,begin,pageSize);
    }
}
